const util = {
    // 获取原型链
    prototype(node) {
        let prototypes = []

        if (!node.__proto__) return []

        prototypes.push(node.__proto__, ...this.prototype(node.__proto__))
        return prototypes
    },
    //input node 判断
    isInputForm(node) {
        return node instanceof HTMLInputElement
    },
    // 添加原型方法
    substr(start, num) {
        return this[util.isInputForm(this) ? 'value' : 'textContent'].substr(start, num)
    },
    //获取节点树
    tree(node) {
        return Array.from(node.childNodes)
            .filter(node => node.nodeType == 1)
            .map(node => ({ tag: node.nodeName, children: this.tree(node) }))
    },
    //获取上面的同辈节点
    previousSibling(element) {
        let nodes = []
        while ((element = element.previousSibling)) {
            if (element.nodeType == 1) nodes.push(element)
        }

        return nodes
    },
    //所有同辈节点
    siblings(node) {
        return Array.from(node.parentNode.childNodes).filter(n => n.nodeType == 1)
        // return [...this.previousSibling, ...this.nextSibling]
    },
    siblingsByTagName(node, tagName) {
        return this.siblings(node).filter(node => {
            return node.tagName == tagName.toUpperCase()
        })
        // return [...this.previousSibling, ...this.nextSibling]
    },
    //删除元素,支持单个元素与元素集合
    removeChild(element, node) {
        const nodes = Array.isArray(node) ? node : [node]
        return nodes.forEach(n => element.removeChild(n))
    },
    createElement(tag, options) {
        const element = document.createElement(tag)
        for (const [key, value] of Object.entries(options)) {
            element[key] = value
        }
        return element
    },
    //----------------------作业---------------------------------------
    //删除子元素中包含xx内容的
    //uitl.removeHasContent(app,a,'tom')   div#app>section>main>a>"tom"
    removeChildNodeHasContent(node, content) {},
    //下面的同辈兄弟节点
    nextSibling(node) {},
}

export default util
